package com.toune.villager.fragment

import android.os.Handler
import com.tamsiree.rxkit.view.RxToast
import com.toune.villager.R
import com.toune.villager.model.HomeFragmentModel
import com.toune.dltools.ui.DLBaseFragment
import com.toune.villager.adapter.AdapterMyInfoList
import com.toune.villager.bean.MyInfoBean
import kotlinx.android.synthetic.main.fragment_message.*
import kotlinx.android.synthetic.main.fragment_message.view.*


class MessageFragment : DLBaseFragment<HomeFragmentModel>() {
    override val layout: Int
        get() = R.layout.fragment_message

    override fun lazyInit() {
        for (i in 0..10) {
            var dataBean = MyInfoBean.DataBean()
            dataBean.type = 1
            msgList.add(dataBean)
        }
        notifyAdapter()
        swipeRefreshLv.setOnRefreshListener {
            refreData()
            RxToast.showToast("下拉刷新$page")
        }
    }

    var msgList: MutableList<MyInfoBean.DataBean> = ArrayList<MyInfoBean.DataBean>()
    var adapterMessageList: AdapterMyInfoList? = null

    private fun notifyAdapter() {
        if (adapterMessageList == null) {
            adapterMessageList = AdapterMyInfoList(msgList)
            mRootView!!.messageRcl.layoutManager =
                androidx.recyclerview.widget.LinearLayoutManager(context)
            mRootView!!.messageRcl.adapter = adapterMessageList
            adapterMessageList!!.setEmptyView(R.layout.layout_empty)
            adapterMessageList!!.loadMoreModule.setOnLoadMoreListener {
                loadMore()
                RxToast.showToast("加载更多$page")
            }
        } else {
            adapterMessageList!!.notifyDataSetChanged()
        }
    }

    var page = 1
    fun loadMore() {
        page++
        Handler().postDelayed({
            adapterMessageList!!.loadMoreModule.loadMoreEnd()
        }, 2000)
    }

    fun refreData() {
        page = 1
        Handler().postDelayed({
            swipeRefreshLv.isRefreshing = false
        }, 2000)
    }
}